package com.taotao.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.common.EasyUITreeNavResult;
import com.taotao.mapper.TreenavMapper;
import com.taotao.pojo.Treenav;
import com.taotao.pojo.TreenavExample;
import com.taotao.pojo.TreenavExample.Criteria;
import com.taotao.service.TreeNavService;

@Service
 public class TreeNavServiceImpl implements TreeNavService{
	@Autowired
	private TreenavMapper treenavMapper;
	
	@Override
	public List<EasyUITreeNavResult> getTreeNav(long treeNavId) {
		List<EasyUITreeNavResult> result = new ArrayList<EasyUITreeNavResult>();
		// 根据treeNavId查询分类列表
		TreenavExample treenavExample = new TreenavExample();
		Criteria createCriteria = treenavExample.createCriteria();
		createCriteria.andParentIdEqualTo(treeNavId);
		// 执行查询
		List<Treenav> list = treenavMapper.selectByExample(treenavExample);
		// 转换成EasyUITreeNavResult
		for (Treenav treenav : list) {
			EasyUITreeNavResult easyUITreeNavResult = new EasyUITreeNavResult();
			easyUITreeNavResult.setId(treenav.getId());
			easyUITreeNavResult.setText(treenav.getName());
			easyUITreeNavResult
					.setState(treenav.getIsParent() ? "closed" : "");
			easyUITreeNavResult.setIconCls(treenav.getIconcls());
			// 构建attributes
			Map<String,Object> attributes = new HashMap<String, Object>();
			attributes.put("url", treenav.getAttributes());
			easyUITreeNavResult.setAttributes(attributes);
			result.add(easyUITreeNavResult);
		}
		return result;
	}

	@Override
	public List<EasyUITreeNavResult> getTreeNav(long treeNavId, long mainNavId) {
		List<EasyUITreeNavResult> result = new ArrayList<EasyUITreeNavResult>();
		// 根据treeNavId查询分类列表
		TreenavExample treenavExample = new TreenavExample();
		Criteria createCriteria = treenavExample.createCriteria();
		createCriteria.andParentIdEqualTo(treeNavId);
		createCriteria.andMnavIdEqualTo(mainNavId);
		// 执行查询
		List<Treenav> list = treenavMapper.selectByExample(treenavExample);
		// 转换成EasyUITreeNavResult
		for (Treenav treenav : list) {
			EasyUITreeNavResult easyUITreeNavResult = new EasyUITreeNavResult();
			easyUITreeNavResult.setId(treenav.getId());
			easyUITreeNavResult.setText(treenav.getName());
			easyUITreeNavResult
					.setState(treenav.getIsParent() ? "closed" : "");
			easyUITreeNavResult.setIconCls(treenav.getIconcls());
			// 构建attributes
			Map<String,Object> attributes = new HashMap<String, Object>();
			attributes.put("url", treenav.getAttributes());
			easyUITreeNavResult.setAttributes(attributes);
			result.add(easyUITreeNavResult);
		}
		return result;
	}

}
